%%%-------------------------------------------------------------------
%%% @author Rain
%%% @doc 角色管理进程
%%% @end
%%%-------------------------------------------------------------------
-module(role_mgr_svr).

-behaviour(game_server).
-include("common.hrl").
-include("role.hrl").
-include("role_mgr.hrl").

-export([start_link/0]).
-export([init/1, do_call/3, do_cast/2, do_info/2, terminate/2,
    code_change/3]).
-export([update_role_info/1]).
-export([get_role_pid/1]).

%%%===================================================================
%%% Spawning and gen_server implementation
%%%===================================================================

start_link() ->
    game_server:start_link(?MODULE, [], []).

init([]) ->
    create_tables(),
    {ok, []}.

do_call(_Request, _From, State) ->
    {noreply, State}.

do_cast(_Request, State) ->
    {noreply, State}.

do_info(_Info, State) ->
    {noreply, State}.

terminate(_Reason, _State) ->
    ok.

code_change(_OldVsn, State, _Extra) ->
    {ok, State}.


%%%===================================================================
%%% Internal functions
%%%===================================================================

%% @doc 启动角色管理相关的ets进程
create_tables() ->
    ets:new(?ETS_ROLE_MGR_INFO, [set, {keypos, #e_role_mgr_info.role_id},
        public, named_table, {read_concurrency, true}]),
    ok.

%% @doc 更新角色信息(角色进程)
update_role_info(RoleId) ->
    Pid = self(),
    Data = #e_role_mgr_info{role_id = RoleId, pid = Pid},
    ets:insert(?ETS_ROLE_MGR_INFO, Data),
    ok.

%% @doc 获取角色进程pid
get_role_pid(RoleId) ->
    case ets:lookup(?ETS_ROLE_MGR_INFO, RoleId) of
        [#e_role_mgr_info{pid = Pid}] ->
            Pid;
        _ ->
            undefined
    end.


